﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace homework6
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 创建闹钟对象
            AlarmClock alarmClock = new AlarmClock();

            // 订阅Tick事件
            alarmClock.Tick += (sender, e) => Console.WriteLine("嘀嗒...");

            // 订阅Alarm事件
            alarmClock.Alarm += (sender, e) => Console.WriteLine($"闹钟响铃！提示信息：{e.Message}");

            // 启动闹钟
            alarmClock.Start();
            Console.ReadKey();
        }
    }
    public class AlarmEventArgs:EventArgs
    {
        public string Message { get; }
        public AlarmEventArgs(string message)
        {
            Message=message;
        }
    }
    public class AlarmClock
    {
        public delegate void TickEventHandler(object sender, EventArgs e);
        public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);
        public event TickEventHandler Tick;
        public event AlarmEventHandler Alarm;
        protected virtual void OnTick(EventArgs e)
        {
            Tick?.Invoke(this, e);
        }
        protected virtual void OnAlarm(AlarmEventArgs e)
        {
            Alarm?.Invoke(this, e);
        }
        public void Start()
        {
            Console.WriteLine("闹钟已启动...");
            while (true)
            {
                // 模拟时间流逝
                Thread.Sleep(1000);

                // 触发Tick事件
                OnTick(EventArgs.Empty);

                // 检查是否到达闹钟时间
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute == 0 && DateTime.Now.Second == 0)
                {
                    // 触发Alarm事件
                    OnAlarm(new AlarmEventArgs("早安！"));
                    break; // 响铃后停止闹钟
                }
            }
        }
    }

}
